package cn.stronglink.asset.message.mq.dam;

import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import cn.stronglink.asset.module.dam.entity.message.MsgScreenCommand;
import cn.stronglink.asset.module.dam.interfaces.CommandStrategyI;
import cn.stronglink.asset.module.dam.service.ScreenReqGetAssetInfoAtRackService;
import cn.stronglink.asset.module.dam.service.ScreenReqGetNetworkParamService;
import cn.stronglink.asset.module.dam.service.ScreenReqGetRackInfoService;
import cn.stronglink.asset.module.dam.service.ScreenReqSetNetworkService;
import cn.stronglink.asset.module.dam.service.ScreenReqSetSensorTypeService;
import cn.stronglink.asset.module.dam.utils.dataJsonUtils.req.receive.GetCmdFromStr;

@Component("commandStrategyManager")
public class CommandReceiver implements MessageListener {
	protected final Logger logger = LoggerFactory.getLogger(CommandReceiver.class);
	public static boolean isNetowrkParam = false;// 是否发送--获取netWork文本的--数据返回
	public static int viewParam = -1;//页面操作参数： 上一页、下一页---供命令为3时，调用此参数
	public static int currPage = -1;
	
	@Autowired
	private ScreenReqGetRackInfoService getRackInfoService;// 2: 机柜信息
	@Autowired
	private ScreenReqGetAssetInfoAtRackService getAssetInfoAtRackService;// 3: 资产信息
	@Autowired
	private ScreenReqGetNetworkParamService getNetworkService;//// 命令4： 获取网络原 参数
	@Autowired
	private ScreenReqSetNetworkService setNetworkService;// 命令222： 设置网络
	@Autowired
	private ScreenReqSetSensorTypeService setSensorTypeService;// 命令333： 设置传感器类型

	private List<CommandStrategyI> strategys = new ArrayList<CommandStrategyI>();

	@Override
	public void onMessage(Message message) {
		if (!(message instanceof ActiveMQTextMessage)) {
			return;
		}

		if (strategys.isEmpty()) {
			strategys.add(setNetworkService);
			strategys.add(getRackInfoService);
			strategys.add(getAssetInfoAtRackService);
			strategys.add(setSensorTypeService);
			strategys.add(getNetworkService);
		}

		TextMessage tm = (TextMessage) message;
		MsgScreenCommand bm = null;
		
		try {
			String msg = tm.getText();
			System.out.println("收到数据：----" + msg);

			if ("".equals(msg) || "null".equals(msg) || msg == null) {
				logger.info("**无效数据**");
				return;
			}
			// 取出一级消息类DamMessage数据
			bm = JSON.parseObject(msg, MsgScreenCommand.class);
		} catch (JMSException e) {
			e.printStackTrace();
		}

		
		if(bm==null) {return;}
		// 获取命令类型： 2设置网络，3设置传感器类型，，，
		String command = bm.getAwsPostdata().getCommand();
		List<Integer> params = GetCmdFromStr.getRequesetInfo(command);
		int commandType = -1;
		if (params.size() > 0) {
			commandType = params.get(0);
			// 资产信息页面： 上一页 下一页
			if (commandType == 3 && params.size() > 1) {
				viewParam = params.get(1);
			}
			System.out.println("cmdType===>" + commandType + " 页面操作参数： " + viewParam);
		}
		
		// 调用service处理请求
		String devType = bm.getDevType();
		String actionCode = bm.getActioncode();
		System.out.println("头参数:===devType  " + devType + "  actionCode----" + actionCode);
		if ("damDc".equals(devType) && "screenRequest".equals(actionCode)) {
			bm.setActioncode("screenCommand");// 供以后：使用消息对象转发指令时使用

			// 二次返回的原网络参数，获取后给控件赋值，使显示原数据
			if (isNetowrkParam) {
				getNetworkService.setTxtValue(bm);
				return;
			}

			// service处理各自的请求
			logger.info("**数据**:" + bm.getAwsPostdata().toString());
			for (CommandStrategyI ser : strategys) {
				if (ser.isHandle(commandType)) {
					if(commandType==3&&viewParam!=-1) {//3资产列表页：翻页操作
						
					}
					ser.handleDamDatas(bm);
				}
			}

		}//if devType/actionCode
		
		
	}
}